package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.domain.YyProject;
import com.ruoyi.gcgl.domain.YyProjectMilestone;
import com.ruoyi.gcgl.domain.YyProjectQrsk;
import com.ruoyi.gcgl.domain.YyProjectSqkp;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.gcgl.service.IYyProjectMilestoneService;
import com.ruoyi.gcgl.service.IYyProjectQrskService;
import com.ruoyi.gcgl.service.IYyProjectService;
import com.ruoyi.gcgl.service.IYyProjectSqkpService;

/**
 * 确认收款Controller
 * 
 * @author ruoyi
 * @date 2023-01-02
 */
@Controller
@RequestMapping("/yy/qrsk")
public class YyProjectQrskController extends BaseController
{
    private String prefix = "yy/qrsk";

    @Autowired
    private IYyProjectQrskService gcProjectQrskService;
    @Autowired
    private IYyProjectService gcProjectService;
    @Autowired
    private IYyProjectSqkpService gcProjectSqkpService;
    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;
    @Autowired
    private IYyProjectMilestoneService sjProjectMilestoneService;

    @GetMapping("/{sqkpId}")
    public String qrsk(ModelMap mmap,@PathVariable("sqkpId") Long sqkpId){
    	mmap.put("sqkpId", sqkpId);
        return prefix + "/qrsk";
    }
    
    /**
     * 查询确认收款列表
     */
    @PostMapping("/list/{sqkpId}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("sqkpId") Long sqkpId)
    {
        startPage();
        YyProjectQrsk gcProjectQrsk = new YyProjectQrsk();
        gcProjectQrsk.setDeleteFlag(1);
        gcProjectQrsk.setSqkpId(sqkpId);
        List<YyProjectQrsk> list = gcProjectQrskService.selectGcProjectQrskList(gcProjectQrsk);
        return getDataTable(list);
    }
    
    @GetMapping()
    public String qrsk4lis(ModelMap mmap){
        return prefix + "/qrsk4lis";
    }
    
    /**
     * 查询确认收款列表
     */
    @PostMapping("/list4lis")
    @ResponseBody
    public TableDataInfo list4lis(YyProjectQrsk gcProjectQrsk)
    {
        startPage();
        gcProjectQrsk.setDeleteFlag(1);
        List<YyProjectQrsk> list = gcProjectQrskService.selectGcProjectQrskList(gcProjectQrsk);
        return getDataTable(list);
    }

    /**
     * 导出确认收款列表
     */
    @Log(title = "确认收款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YyProjectQrsk gcProjectQrsk)
    {
        List<YyProjectQrsk> list = gcProjectQrskService.selectGcProjectQrskList(gcProjectQrsk);
        ExcelUtil<YyProjectQrsk> util = new ExcelUtil<YyProjectQrsk>(YyProjectQrsk.class);
        return util.exportExcel(list, "确认收款数据");
    }

    /**
     * 新增确认收款
     */
    @GetMapping("/add/{sqkpId}")
    public String add(ModelMap mmap,@PathVariable("sqkpId") Long sqkpId){
    	YyProjectSqkp sqkp = gcProjectSqkpService.selectGcProjectSqkpById(sqkpId);
    	mmap.put("sqkp", sqkp);
    	
    	YyProjectQrsk gcProjectQrsk = new YyProjectQrsk();
    	gcProjectQrsk.setDeleteFlag(1);
    	gcProjectQrsk.setSqkpId(sqkpId);
    	List<YyProjectQrsk> lis = gcProjectQrskService.selectGcProjectQrskList(gcProjectQrsk);
    	
    	double bcsqkpje = Double.parseDouble(sqkp.getItemje().toString());
    	for(YyProjectQrsk qrsk:lis){
    		bcsqkpje -= Double.parseDouble(qrsk.getQrjine().toString());
    	}
    	mmap.put("qrjine", bcsqkpje);
        return prefix + "/add";
    }

    /**
     * 新增保存确认收款
     */
    @Log(title = "确认收款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YyProjectQrsk gcProjectQrsk){
    	gcProjectQrsk.setDeleteFlag(1);
    	gcProjectQrsk.setCreateAt(new Date());
    	gcProjectQrsk.setCreateUser(getLoginName());
    	gcProjectQrsk.setUpdateAt(new Date());
    	gcProjectQrsk.setUpdateUser(getLoginName());
    	gcProjectQrskService.insertGcProjectQrsk(gcProjectQrsk);
    	
    	//计算是否更新收款状态
    	YyProjectQrsk obj = new YyProjectQrsk();
    	obj.setDeleteFlag(1);
    	obj.setSqkpId(gcProjectQrsk.getSqkpId());
    	List<YyProjectQrsk> lis = gcProjectQrskService.selectGcProjectQrskList(obj);
    	
    	YyProjectSqkp sqkp = gcProjectSqkpService.selectGcProjectSqkpById(gcProjectQrsk.getSqkpId());
    	double bcsqkpje = Double.parseDouble(sqkp.getItemje().toString());
    	for(YyProjectQrsk qrsk:lis){
    		bcsqkpje -= Double.parseDouble(qrsk.getQrjine().toString());
    	}
    	if(bcsqkpje==0){
    		YyProjectSqkp gcProjectSqkp = new YyProjectSqkp();
    		gcProjectSqkp.setId(gcProjectQrsk.getSqkpId());
    		gcProjectSqkp.setStatus(9);
    		gcProjectSqkp.setUpdateUser(getLoginName());
    		gcProjectSqkp.setUpdateAt(new Date());
    		gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
    	}
    	
        return toAjax(1);
    }

    /**
     * 修改确认收款
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YyProjectQrsk gcProjectQrsk = gcProjectQrskService.selectGcProjectQrskById(id);
        mmap.put("gcProjectQrsk", gcProjectQrsk);
        return prefix + "/edit";
    }

    /**
     * 修改保存确认收款
     */
    @Log(title = "确认收款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YyProjectQrsk gcProjectQrsk){
    	gcProjectQrsk.setUpdateUser(getLoginName());
    	gcProjectQrsk.setUpdateAt(new Date());
    	gcProjectQrskService.updateGcProjectQrsk(gcProjectQrsk);
    	
    	//计算是否更新收款状态
    	YyProjectQrsk obj = new YyProjectQrsk();
    	obj.setDeleteFlag(1);
    	obj.setSqkpId(gcProjectQrsk.getSqkpId());
    	List<YyProjectQrsk> lis = gcProjectQrskService.selectGcProjectQrskList(obj);
    	
    	YyProjectSqkp sqkp = gcProjectSqkpService.selectGcProjectSqkpById(gcProjectQrsk.getSqkpId());
    	double bcsqkpje = Double.parseDouble(sqkp.getItemje().toString());
    	for(YyProjectQrsk qrsk:lis){
    		bcsqkpje -= Double.parseDouble(qrsk.getQrjine().toString());
    	}
    	if(bcsqkpje==0){
    		YyProjectSqkp gcProjectSqkp = new YyProjectSqkp();
    		gcProjectSqkp.setId(gcProjectQrsk.getSqkpId());
    		gcProjectSqkp.setStatus(9);
    		gcProjectSqkp.setUpdateUser(getLoginName());
    		gcProjectSqkp.setUpdateAt(new Date());
    		gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
    	}
    	
        return toAjax(1);
    }

    /**
     * 删除确认收款
     */
    @Log(title = "确认收款", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectQrskService.deleteGcProjectQrskByIds(ids));
    }
    
    @GetMapping("/p4qrsk/{id}")
    public String p4qrsk(ModelMap mmap,@PathVariable("id") Long id){
    	YyProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//--------------------应收
    	String phase = "";//阶段
    	double yingshoujine = 0.0;
    	YyProjectMilestone sjProjectMilestone = new YyProjectMilestone();
    	sjProjectMilestone.setDeleteFlag(1);
    	sjProjectMilestone.setGcProjectid(id);
    	sjProjectMilestone.setStatus(1);//1完成 0未完成
    	List<YyProjectMilestone> mlis = sjProjectMilestoneService.selectSjProjectMilestoneList(sjProjectMilestone);
    	for(YyProjectMilestone mo : mlis){
    		yingshoujine += mo.getJine()==null?0.0:mo.getJine().doubleValue();
    		phase = mo.getName();
    	}
    	
    	//-------------------已收费用
    	double yishoujine = 0.0;
    	YyProjectQrsk gcProjectQrsk = new YyProjectQrsk();
    	gcProjectQrsk.setDeleteFlag(1);
    	gcProjectQrsk.setGcProjectid(id);
    	List<YyProjectQrsk> qrsklis = gcProjectQrskService.selectGcProjectQrskList(gcProjectQrsk);
    	for(YyProjectQrsk qr : qrsklis){
    		yishoujine += qr.getQrjine().doubleValue();
    	}
    	
    	mmap.put("yingshoujine", yingshoujine);//应收
    	mmap.put("yishoujine", yishoujine);//已收费用
    	
    	if(yingshoujine!=0.0 && yishoujine!=0.0){
    		mmap.put("skbl", DecimalCalculateUtil.div(yishoujine, yingshoujine, 2)+"%");//收款比例
    	}else{
    		mmap.put("skbl", 0+"%");//收款比例
    	}
    	
    	if(yingshoujine > yishoujine){
    		mmap.put("tip", "收款滞后");//提示信息 收款正常/收款滞后
    	}else{
    		mmap.put("tip", "收款正常");//提示信息 收款正常/收款滞后
    	}
    	mmap.put("phase", phase);//阶段
    	
    	//-------------------
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
    	return "yy/project/projectDetailQrsk";
    }
    
    /**
     * 查询确认收款列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(YyProjectQrsk gcProjectQrsk)
    {
        startPage();
        gcProjectQrsk.setDeleteFlag(1);
        List<YyProjectQrsk> list = gcProjectQrskService.selectGcProjectQrskList(gcProjectQrsk);
        return getDataTable(list);
    }
}